import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Table, Row, Col, Form, Input, Radio, Select, Button, message, Popconfirm } from 'antd';
const R = require('ramda');
import BizParams from '../../../components/commons/BizParams'
import OrderDetail from '../../../components/order/components/OrderDetail';
import RightBox from '../../../components/commons/RightBox';
import RightConstants from '../../../utils/RightConstants';
import Constants from '../../../utils/Constants';

const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
export default connect(({ orderManage }) => ({ orderManage }))(
    Form.create()(class OrderQuery extends React.Component {
        constructor(props) {
            super(props);
        }

        // componentDidMount() {
        //     this.props.dispatch({ type: 'orderManage/queryOrderByNo' });//查询订单列表
        // }

        collectSearchCondition = () => {
            let conditions = this.props.form.getFieldsValue();
            //conditions.page = this.props.orderManage.page;
            return conditions;
        }

        onView = (record) => {
            this.handleOpenOrderDetail(record, true, true, true, true, {}, "1");
        }

        // onEdit = (record) => {
        //     this.handleOpenOrderDetail(record, false, true, true, false, this.collectSearchCondition(), "1");
        // }

        // onUploadConfirmLetter = (record) => {
        //     this.handleOpenOrderDetail(record, true, false, true, true, this.collectSearchCondition(), "5");
        // }

        // onEditCost = (record) => {
        //     this.handleOpenOrderDetail(record, true, true, false, true, this.collectSearchCondition(), "6");
        // }

        // onManageAttachment = (record) => {
        //     this.handleOpenOrderDetail(record, true, true, true, false, this.collectSearchCondition(), "4");
        // }

        handleOpenOrderDetail = (record, orderDisabled, letterDisabled, costDisabled, attachmentDisabled, searchConditions, tabActiveKey) => {
            this.props.dispatch({ type: 'orderManage/getOrderById', payload: { id: record.id } });//获取订单详情
            this.props.dispatch({ type: 'orderManage/queryDrawerList', payload: { cstId: record.cstid } });//查询开票人列表
            this.props.dispatch({ type: 'orderCost/queryOrderCost', payload: { id: record.id, status: "1" } });//查询费用列表
            this.props.dispatch({ type: 'orderManage/queryLinkmanList', payload: { cstId: record.cstid } });//查询联系人列表
            this.props.dispatch({ type: 'orderManage/queryAddressList', payload: { cstId: record.cstid } });//查询货物存放地址列表
            this.props.dispatch({ type: 'orderManage/queryBuyerList', payload: { cstId: record.cstid } });//查询境外买家
            this.props.dispatch({ type: 'orderManage/querySupplyGoods', payload: { cstId: record.cstid } });//查询境内货源地
            this.props.dispatch({ type: 'orderManage/queryTgManagers' });//查询通关专员
            this.refs.orderDetail.handleOpen(orderDisabled, letterDisabled, costDisabled, attachmentDisabled, searchConditions, tabActiveKey);
        }

        handleSearch = (e) => {
            let values = this.props.form.getFieldsValue();
            // let newPage = this.props.orderManage.page;
            // newPage.page = 1;
            this.props.dispatch({
                type: 'orderManage/queryOrderByNo',
                payload: {
                    orderNo: values.orderNo,
                    contractNo: values.contractNo,
                    //page: newPage
                }
            });
        };

        // handleCopy = (record) => {
        //     this.props.dispatch({
        //         type: 'orderManage/copyOrderById',
        //         payload: {
        //             id: record.id,
        //             searchConditions: this.collectSearchCondition()
        //         }
        //     });
        // }

        // handleDelete = (record) => {
        //     this.props.dispatch({
        //         type: 'orderManage/deleteOrderById',
        //         payload: {
        //             id: record.id,
        //             searchConditions: this.collectSearchCondition()
        //         }
        //     });
        // }

        // handleReturnOrder = (record) => {
        //     this.props.dispatch({
        //         type: 'orderManage/returnOrder',
        //         payload: {
        //             id: record.id,
        //             searchConditions: this.collectSearchCondition(),
        //             gobackreason: Constants.RETURN_ORDER
        //         }
        //     });
        // }

        columns = [
            { title: '订单编号', dataIndex: 'orderno', key: 'orderno' },
            { title: '客户名称', dataIndex: 'companyname', key: 'companyname' },
            { title: '合同协议号', dataIndex: 'contractno', key: 'contractno' },
            { title: '报关方式', dataIndex: 'declarationTypeName', key: 'declarationTypeName' },
            { title: '报关口岸', dataIndex: 'portofentryidName', key: 'portofentryidName' },
            { title: '总货值', dataIndex: 'totleamount', key: 'totleamount' },
            { title: '币种', dataIndex: 'currencytype', key: 'currencytype' },
            { title: '提交人', dataIndex: 'creatorName', key: 'creatorName' },
            { title: '提交时间', dataIndex: 'createtime', key: 'createtime', },
            { title: '预计报关时间', dataIndex: 'exporttime', key: 'exporttime' },
            {
                title: '状态', dataIndex: 'status', key: 'status',
                render: (text, record, index) => {
                    return (Constants.ORDERSTATUS[record.status])
                }
            },
            {
                title: '操作', key: 'key_oprator',
                render: (text, record, index) => {
                    let split = <span className="ant-divider" />
                    // let copyLink = <Popconfirm placement="leftTop" onConfirm={this.handleCopy.bind(this, record)} okText="是" cancelText="否"
                    //     title={'复制原订单的一份草稿, 保留原订单附件之外的所有基本信息, 订单号生成方式会默认为系统生成, 是否继续复制?'}>
                    //     <a href="#">复制</a>
                    // </Popconfirm>
                    // copyLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_COPY} split={true}>{copyLink}</RightBox>
                    // let costLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_COST} split={true}><a href="#" onClick={this.onEditCost.bind(this, record)}>费用</a></RightBox>
                    let viewLink = <a href="#" onClick={this.onView.bind(this, record)}>查看</a>
                    //let editLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_EDIT} split={true}><a href="#" onClick={this.onEdit.bind(this, record)}>编辑</a></RightBox>
                    // let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record)} okText="是" cancelText="否"
                    //     placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'}>
                    //     <a href="#">删除</a>
                    // </Popconfirm>
                    // deleteLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_DELETE} split={true}>{deleteLink}</RightBox>
                    //let confirmLetterLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_LETTER} split={true}><a href="#" onClick={this.onUploadConfirmLetter.bind(this, record)}>确认函</a></RightBox>
                    // let attachmentLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_ATTACH} split={true}><a href="#" onClick={this.onManageAttachment.bind(this, record)}>附件</a></RightBox>
                    // let returnLink = <Popconfirm onConfirm={this.handleReturnOrder.bind(this, record)} okText="是" cancelText="否"
                    //     placement="leftTop" title={'订单将退回待补充资料状态，包括费用，已关联外汇，通关信息，预开票在内的信息都会被删除，是否继续？'}>
                    //     <a href="#">退回</a>
                    // </Popconfirm>
                    // returnLink = <RightBox resource={RightConstants.ORDER.ORDER_MANAGE_RETURN_ORDER} split={true}>{returnLink}</RightBox>

                    // let operaters = {
                    //     "1": <span>{copyLink}{costLink}{editLink}{deleteLink}{viewLink}</span>,
                    //     "2": <span>{copyLink}{costLink}{attachmentLink}{viewLink}</span>,
                    //     "3": <span>{copyLink}{costLink}{editLink}{deleteLink}{viewLink}</span>,
                    //     "4": <span>{copyLink}{costLink}{confirmLetterLink}{attachmentLink}{viewLink}</span>,
                    //     "5": <span>{copyLink}{costLink}{attachmentLink}{returnLink}{viewLink}</span>,
                    //     "6": <span>{copyLink}{costLink}{attachmentLink}{viewLink}</span>,
                    //     "7": <span>{copyLink}{viewLink}</span>
                    // }
                    // return (operaters[record.status]);
                    return <span>{viewLink}</span>
                }
            }];

        render() {
            let dataSource = R.isNil(this.props.orderManage.dataSource) ? {} : this.props.orderManage.dataSource;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            // let pagination = {
            //     showTotal: total => `共 ${total} 条数据`,
            //     current: this.props.orderManage.page.page,
            //     pageSize: this.props.orderManage.page.size,
            //     total: this.props.orderManage.page.totalRow,
            //     showSizeChanger: true,
            //     onShowSizeChange: (current, size) => {
            //         let newPage = this.props.orderManage.page;
            //         newPage.size = size;
            //         newPage.page = 1;
            //         this.props.dispatch({
            //             type: 'orderManage/queryOrderManageList',
            //             payload: {
            //                 cstName: values.cstName,
            //                 orderNo: values.orderNo,
            //                 contractNo: values.contractNo,
            //                 declarationType: values.declarationType,
            //                 status: values.status,
            //                 page: newPage
            //             }
            //         });
            //     },
            //     onChange: (pageNumber, pageSize) => {
            //         let newPage = this.props.orderManage.page;
            //         newPage.page = pageNumber;
            //         this.props.dispatch({
            //             type: 'orderManage/queryOrderManageList',
            //             payload: {
            //                 cstName: values.cstName,
            //                 orderNo: values.orderNo,
            //                 contractNo: values.contractNo,
            //                 declarationType: values.declarationType,
            //                 status: values.status,
            //                 page: newPage
            //             }
            //         });
            //     }
            // }
            return (
                <div>
                    <Row>
                        {/* <Col span={6}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col> */}
                        <Col span={6}><FormItem {...formItemLayout} label={'订单编号'}>{getFieldDecorator('orderNo')(<Input />)}</FormItem></Col>
                    {/* </Row>
                    <Row> */}
                        <Col span={6}><FormItem {...formItemLayout} label={'合同协议号'}>{getFieldDecorator('contractNo')(<Input />)}</FormItem></Col>
                        {/* <Col span={6}><FormItem {...formItemLayout} label={'报关方式'}>
                            {getFieldDecorator('declarationType')(
                                <BizParams sprCode={"declarationtype"} style={{ width: '100%' }} all={true} />
                            )}
                        </FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <FormItem {...formItemLayout} label={'状态'}>
                                {getFieldDecorator('status', {
                                    initialValue: ""
                                })(
                                    <RadioGroup style={{ width: 800 }}>
                                        <Radio value="">全部</Radio>
                                        <Radio value="1">草稿</Radio>
                                        <Radio value="2">待通关受理</Radio>
                                        <Radio value="3">待补充资料</Radio>
                                        <Radio value="4">待签确认函</Radio>
                                        <Radio value="5">待报关</Radio>
                                        <Radio value="6">报关中</Radio>
                                        <Radio value="7">办理完毕</Radio>
                                    </RadioGroup>
                                    )}
                            </FormItem>
                        </Col> */}
                        <Col span={6} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, null)}>查询</Button>
                        </Col>
                    </Row>
                    <Table rowKey={record => record.id} dataSource={this.props.orderManage.orderQueryList} columns={this.columns} bordered={true} size={'middle'} pagination={false} />
                    <OrderDetail ref={"orderDetail"} cstId={dataSource.cstid} {...this.props.orderManage} dispatch={this.props.dispatch} />
                </div>
            )
        }
    })
)
